package com.blue.utils;

import java.io.*;

/**
 * Created by Administrator on 2019/7/24.
 */
public class JsonUtils {
    public static boolean falg = false;

    /**
     * 读取json文件
     */
    public static String readJsonFile(String path) {
        String laststrJson = "";
        BufferedReader reader;
        try {
            System.out.println(  " globalParams.json file position " + path);
            reader = new BufferedReader( new FileReader( path ) );
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                laststrJson = laststrJson + tempString;
                line++;
            }
            reader.close();
        } catch (IOException e1) {
            //e1.printStackTrace();
            System.out.println( "load globalParams.json again" );
            readJsonFile( path );
        }
        return laststrJson;
    }

    /**
     * 写出json文件
     */
    public static boolean writeJsonFile(String newJsonString, String path,String pathNew) {
        try {
            FileWriter fw = new FileWriter( pathNew );
            PrintWriter out = new PrintWriter( fw );
            out.write( newJsonString );
            out.println();
            fw.close();
            out.close();
            System.out.println( "succeed" );
            System.out.println( pathNew );
            File filePro = new File( path );
            filePro.delete();
            File fileNew = new File( pathNew );
            fileNew.renameTo( filePro );
            falg = true;
            return falg;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println( "update front web globalParams.json again " );
            writeJsonFile( newJsonString, path ,pathNew);
        }
        return falg;
    }



}
